package com.yalemang.photovalut.ui.main

import android.annotation.SuppressLint
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.ToastUtils
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.yalemang.photovalut.R
import com.yalemang.photovalut.base.BaseSkinActivity
import com.yalemang.photovalut.base.ext.gone
import com.yalemang.photovalut.base.ext.visible
import com.yalemang.photovalut.base.user.Skin
import com.yalemang.photovalut.base.utils.SvgUtils
import com.yalemang.photovalut.databinding.ActivityMainBinding
import com.yalemang.photovalut.databinding.ViewMainTopNavigationBarItemBinding
import com.yalemang.photovalut.res.R.*
import com.yalemang.photovalut.ui.calculator.CalculatorActivity
import com.yalemang.photovalut.ui.main.adapter.MainFragmentAdapter
import com.yalemang.photovalut.ui.main.adapter.MainDrawerAdapter
import com.yalemang.photovalut.ui.main.page.photo.PhotoFragment
import com.yalemang.photovalut.ui.main.page.video.VideoFragment
import java.util.HashMap


class MainActivity : BaseSkinActivity<ActivityMainBinding, MainPresenter>(), MainContract.MainView {

    private val tabResList = ArrayList<Int>()
    private val tabTitles = ArrayList<String>()

    @SuppressLint("ResourceType")
    override fun switchSkin(skin: Skin) {
        this.skin = skin
        mBinding.tb.setBackgroundColor(skin.mainColor)

        val currentPosition = mBinding.vp2.currentItem
        val customView = mBinding.tlTab.getTabAt(currentPosition)!!.customView
        val binding = ViewMainTopNavigationBarItemBinding.bind(customView!!)
        binding.ivNavigationBar.setImageDrawable(SvgUtils.getDrawable(skin.mainColor, tabResList[currentPosition]))
        binding.vNavigationBar.setBackgroundColor(skin.mainColor)
    }

    override fun initData() {
        tabResList.add(drawable.ic_photo)
        tabResList.add(drawable.ic_task_video)
        tabTitles.add(getString(string.main_tab_layout_photo))
        tabTitles.add(getString(string.main_tab_layout_video))
        startCheckFileReadWritePermission {
            if (it) {
                ToastUtils.showLong("成功啦")
            } else {
                ToastUtils.showLong("失败啦")
            }
        }
    }

    override fun initView() {
        mBinding.tb.tvLeft.gone()
        mBinding.tb.tvRight.gone()
        mBinding.tb.ivRight.gone()
        mBinding.tb.ivCenter.gone()

        val adRequest = AdRequest.Builder().build()
        mBinding.adView.adListener = object : AdListener() {
            override fun onAdLoaded() {
                super.onAdLoaded()
                Log.d("xing", "成功")
            }

            override fun onAdFailedToLoad(p0: LoadAdError) {
                super.onAdFailedToLoad(p0)
                Log.d("xing", "失败")
            }
        }
        mBinding.adView.loadAd(adRequest)

        val adapter = MainFragmentAdapter(this)
        adapter.fragments.add(PhotoFragment())
        adapter.fragments.add(VideoFragment())
        mBinding.vp2.adapter = adapter

        for (i in 0..1) {
            mBinding.tlTab.addTab(mBinding.tlTab.newTab().setCustomView(getTabView(i)))
        }
        mBinding.tlTab.addOnTabSelectedListener(object : OnTabSelectedListener {
            @SuppressLint("ResourceType", "InflateParams")
            override fun onTabSelected(p0: TabLayout.Tab) {
                mBinding.vp2.currentItem = p0.position
                val view = p0.customView!!
                val tabText = view.findViewById<View>(R.id.tv_navigation_bar) as TextView
                val tabIcon = view.findViewById<View>(R.id.iv_navigation_bar) as ImageView
                val tabV = view.findViewById<View>(R.id.v_navigation_bar)
                tabIcon.setImageDrawable(SvgUtils.getDrawable(skin.mainColor, tabResList[p0.position]))
                tabText.setTextColor(skin.mainColor)
                tabV.visible()
            }

            @SuppressLint("ResourceAsColor")
            override fun onTabUnselected(p0: TabLayout.Tab) {
                val view = p0.customView!!
                val tabText = view.findViewById<View>(R.id.tv_navigation_bar) as TextView
                val tabIcon = view.findViewById<View>(R.id.iv_navigation_bar) as ImageView
                val tabV = view.findViewById<View>(R.id.v_navigation_bar)
                tabIcon.setImageResource(tabResList[p0.position])
                tabText.setTextColor(resources.getColor(color.grey_20, null))
                tabV.gone()
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {

            }
        })
        mBinding.tlTab.setSelectedTabIndicator(R.drawable.tab_indicator)

        mBinding.vp2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                val tab = mBinding.tlTab.getTabAt(position)
                mBinding.tlTab.selectTab(tab)
            }
        })

        mBinding.tb.ivLeft.setOnClickListener{
            mBinding.drawerLayoutMain.openDrawer(GravityCompat.START)
        }

        val itemNameList =  ArrayList<String>()
        itemNameList.add(getString(string.main_drawer_item_to_calculator))
        itemNameList.add(getString(string.main_drawer_item_skin))
        itemNameList.add(getString(string.main_drawer_item_update_password))
        itemNameList.add(getString(string.main_drawer_item_share))
        itemNameList.add(getString(string.main_drawer_item_contact))
        itemNameList.add(getString(string.main_drawer_item_settings))
        itemNameList.add(getString(string.main_drawer_item_about))
        val itemDrawableMap = HashMap<String, Int>()
        itemDrawableMap[itemNameList[0]] = drawable.ic_photo
        itemDrawableMap[itemNameList[1]] = drawable.ic_photo
        itemDrawableMap[itemNameList[2]] = drawable.ic_photo
        itemDrawableMap[itemNameList[3]] = drawable.ic_photo
        itemDrawableMap[itemNameList[4]] = drawable.ic_photo
        itemDrawableMap[itemNameList[5]] = drawable.ic_photo
        itemDrawableMap[itemNameList[6]] = drawable.ic_menu_about

        mBinding.rvMainDrawer.layoutManager = LinearLayoutManager(this)
        val mAdapter = MainDrawerAdapter(itemNameList, itemDrawableMap)
        mBinding.rvMainDrawer.adapter = mAdapter
        mAdapter.itemClickListener = { data: String, position: Int ->
            when(data){
                getString(string.main_drawer_item_to_calculator) ->{
                    val intent = Intent(this@MainActivity, CalculatorActivity::class.java)
                    startActivity(intent)
                    finish()
                }
                getString(string.main_drawer_item_skin) ->{

                }
                getString(string.main_drawer_item_update_password) ->{

                }
                getString(string.main_drawer_item_share) ->{

                }
                getString(string.main_drawer_item_contact) ->{

                }
                getString(string.main_drawer_item_settings) ->{

                }
                getString(string.main_drawer_item_about) -> {

                }
            }
        }
    }

    @SuppressLint("InflateParams")
    fun getTabView(position: Int): View {
        val tabBinding = ViewMainTopNavigationBarItemBinding.inflate(layoutInflater)
        tabBinding.tvNavigationBar.text = tabTitles[position]
        tabBinding.vNavigationBar.setBackgroundColor(skin.mainColor)
        if (position == 0) {
            tabBinding.tvNavigationBar.setTextColor(skin.mainColor)
            tabBinding.vNavigationBar.visible()
        } else {
            tabBinding.ivNavigationBar.setImageResource(tabResList[position])
            tabBinding.vNavigationBar.gone()
        }
        return tabBinding.root
    }

    override fun initMVP() {
        mPresenter = MainPresenter()
        mPresenter.mView = this
        mPresenter.mModel = MainModel()
    }
}